package com.baoyouqun.controller.back;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baoyouqun.base.PageResponse;
import com.baoyouqun.base.utils.ConvertUtils;
import com.baoyouqun.domain.CreateCmd.UserCreateCmd;
import com.baoyouqun.domain.Query.UserDetailQuery;
import com.baoyouqun.domain.Query.UserQuery;
import com.baoyouqun.domain.UpdateCmd.UserUpdateCmd;
import com.baoyouqun.domain.VO.ImportUserVO;
import com.baoyouqun.domain.VO.RoleVO;
import com.baoyouqun.domain.VO.UserDetailVO;
import com.baoyouqun.domain.VO.UserVO;
import com.baoyouqun.domain.cmd.BatchDeleteUserCmd;
import com.baoyouqun.domain.cmd.ExportUserCmd;
import com.baoyouqun.domain.cmd.ResetPasswordCmd;
import com.baoyouqun.domain.cmd.UserRoleCmd;
import com.baoyouqun.entity.Role;
import com.baoyouqun.service.RoleService;
import com.baoyouqun.service.UserRoleService;
import com.baoyouqun.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("back/user")
@Api(tags = "后台_用户管理接口")
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private UserRoleService userRoleService;
    @Resource
    private RoleService roleService;

    @GetMapping("backUserPage")
    @ApiOperation("分页查询后台用户")
    public PageResponse<UserDetailVO> backUserPage(UserQuery query) {
        IPage<UserDetailVO> page = userService.backUserPage(query);
        return PageResponse.of(page.getRecords(), page.getTotal(), page.getCurrent(), page.getSize());
    }


    @DeleteMapping("batchDelete")
    @ApiOperation("批量删除用户")
    public Response batchDelete(@Valid @RequestBody BatchDeleteUserCmd cmd) {
        StpUtil.checkRole("R010000");
        return userService.batchDelete(cmd);
    }

    @PostMapping("resetPassword")
    @ApiOperation("重置用户密码（无需旧密码）")
    public Response resetPassword(@Valid @RequestBody ResetPasswordCmd cmd) {
        return userService.resetPassword(cmd);
    }

    @GetMapping("export")
    @ApiOperation("导出用户列表")
    public void exportUser(@Valid ExportUserCmd cmd, HttpServletResponse response) throws IOException {
        userService.exportUser(cmd, response);
    }

    @GetMapping("getDetail")
    @ApiOperation("查询用户详情（包含角色和权限）")
    public SingleResponse<UserDetailVO> getUserDetail(@Valid UserDetailQuery query) {
        return userService.getUserDetail(query);
    }


    @PostMapping("import")
    @ApiOperation("导入用户列表")
    public SingleResponse<ImportUserVO> importUser(@RequestParam("file") MultipartFile file) throws IOException {
        return userService.importUser(file);
    }


    @PostMapping("create")
    @ApiOperation("新增用户")
    public SingleResponse<UserVO> create(UserCreateCmd cmd) {
        cmd.setBackWorker(true);
        return userService.create(cmd);
    }

    @PutMapping("update")
    @ApiOperation("修改用户")
    public Response update(UserUpdateCmd cmd) {
        return userService.update(cmd);
    }

    @PutMapping("disEnable")
    @ApiOperation("启用&禁用用户")
    public Response disEnable(UserUpdateCmd cmd) {
        return userService.disEnable(cmd);
    }

    @DeleteMapping("delete")
    @ApiOperation("根据ID删除用户")
    public Response delete(String id) {
        StpUtil.checkRole("R010000");
        userService.removeById(id);
        return Response.buildSuccess();
    }

    @GetMapping("getById")
    @ApiOperation("根据ID查询用户")
    public SingleResponse<UserVO> selectById(String id) {
        return SingleResponse.of(userService.selectById(id));
    }

    @GetMapping("page")
    @ApiOperation("分页查询用户")
    public PageResponse<UserVO> page(UserQuery query) {
        IPage<UserVO> page = userService.pageVo(query);
        return PageResponse.of(page.getRecords(), page.getTotal(), page.getCurrent(), page.getSize());
    }

    @PostMapping("assignRoles")
    @ApiOperation("为用户分配角色")
    public Response assignRoles(@Valid @RequestBody UserRoleCmd cmd) {
        return userRoleService.assignRoles(cmd);
    }

    @GetMapping("getUserRoleList")
    @ApiOperation("查询用户拥有的角色")
    public SingleResponse<List<RoleVO>> getUserRoles(String userId) {
        return userService.getUserRoles(userId);
    }

    @GetMapping("getRolesSelectList")
    @ApiOperation("查询角色可选择列表")
    public SingleResponse<List<RoleVO>> getRolesSelectList() {
        List<Role> list = roleService.list().stream().filter(roleVO -> !roleVO.getRoleCode().equals("SUPER_ADMIN")).collect(Collectors.toList());
        return SingleResponse.of(ConvertUtils.copyBeanList(list, RoleVO.class));
    }


}